﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using AutoMapper;
using KCMS.BusinessLogic.Models;
using KCMS.Engine;
namespace KCMS.Controllers
{
    public class ProductController : Controller
    {
        private IProductService pService = ObjectFactory.GetInstance<IProductService>();
        private IFilterProduct productFilter = ObjectFactory.GetInstance<IFilterProduct>();
        private IProductService productService = ObjectFactory.GetInstance<IProductService>();
        private int pageSize = 12;
        public ActionResult Index()
        {
            Guid id = new Guid();
            BusinessLogic.Models.Product product = pService.getById(id);
            var model = Mapper.Map<BusinessLogic.Models.Product, KCMS.Web.Models.ProductViewModel>(product);
            return View(model);
        }
        public ActionResult ShowWithCategory(String code, int index, String category)
        {

            BusinessLogic.Models.Product product = pService.getByCodeIndex(code, index);
            var model = Mapper.Map<BusinessLogic.Models.Product, KCMS.Web.Models.ProductViewModel>(product);
            return View("Show", model);
        }
        public ActionResult Show(String code, int index)
        {
            BusinessLogic.Models.Product product = pService.getByCodeIndex(code, index);
            var model = Mapper.Map<BusinessLogic.Models.Product, KCMS.Web.Models.ProductViewModel>(product);
            return View(model);
        }
        public ActionResult GetProductByArticle(string article)
        {

            IEnumerable<Product> items = pService.searchByArticle(article);
            if (items.Count() == 1)
            {
                return RedirectToAction("Show", new { code = items.First().Code, index = items.First().Index });
            }
            else if (items.Count() != 0)
            {
                var model = Mapper.Map<IEnumerable<BusinessLogic.Models.Product>, IEnumerable<KCMS.Web.Models.ProductViewModel>>(items);
                return View(model);
            }
            ViewBag.Article = article;
            return View("NotFound");
        }

        public ActionResult ShowListByCategory(Guid categoryId, Guid brendId, int pageNum = 0, int filter = 0)
        {
            var productCategory = new List<Product>();
            if (brendId.Equals(Guid.Empty))
            {
                productCategory = (from t in pService.getAllPublished()
                                   where t.CategoryId.Equals(categoryId)
                                   select t).ToList();
            }
            else
            {
                productCategory = (from t in pService.getAllPublished()
                                   where (t.CategoryId.Equals(categoryId) && t.BrendId.Equals(brendId))
                                   select t).ToList();
            }
            var productList = productFilter.getAll(productCategory, filter);
            ViewData["BrendId"] = brendId;
            ViewData["Filter"] = filter;
            ViewData["PageNum"] = pageNum;
            ViewData["ItemsCount"] = (productList != null) ? productList.Count() : 0;
            ViewData["PageSize"] = pageSize;
            var list = productList.Skip(pageSize * pageNum).Take(pageSize).ToList();
            var model = Mapper.Map<IEnumerable<BusinessLogic.Models.Product>, IEnumerable<KCMS.Web.Models.ProductViewModel>>(list);
            if (!Request.IsAjaxRequest())
            {
                return PartialView(model);
            }
            else
            {
                return PartialView("ListWithNavigation", model);
            }
        }
        
    }
}
